﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DataAccessMock
{
    public class DataReaderMock : IDataReader
    {
        private DataSet _dataSet = null;
        private int _indice = 0;

        public DataReaderMock(DataSet dataSet)
        {
            _dataSet = dataSet;
        }

        public void Close()
        {
            _dataSet = null;
        }

        public int Depth
        {
            get { return 0; }
        }

        public DataTable GetSchemaTable()
        {
            if (_dataSet.Tables.Count > 0)
            {
                return _dataSet.Tables[0];
            }

            return null;
        }

        public bool IsClosed
        {
            get
            {
                return _dataSet == null ? true : false;
            }
        }

        public bool NextResult()
        {
            if (_dataSet.Tables.Count > 0)
            {
                if ((_dataSet.Tables[0].Rows.Count - 1) <= _indice)
                {
                    return false;
                }
            }

            _indice++;

            return true;
        }

        public bool Read()
        {
            if (_dataSet.Tables.Count > 0)
            {
                if ((_dataSet.Tables[0].Rows.Count - 1) < _indice)
                {
                    return false;
                }
            }

            _indice++;

            return true;
        }

        public int RecordsAffected
        {
            get
            {
                if (_dataSet.Tables.Count > 0)
                {
                    return _dataSet.Tables[0].Rows.Count;
                }
                else
                {
                    return 0;
                }
            }
        }

        public void Dispose()
        {
            _dataSet = null;
        }

        public int FieldCount
        {
            get
            {
                if (_dataSet.Tables.Count > 0)
                {
                    return _dataSet.Tables[0].Columns.Count;
                }

                return 0;
            }
        }

        public bool GetBoolean(int i)
        {
            return Convert.ToBoolean(this[i]);
        }

        public byte GetByte(int i)
        {
            return Convert.ToByte(this[i]);
        }

        public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
        {
            return 0;
        }

        public char GetChar(int i)
        {
            return Convert.ToChar(this[i]);
        }

        public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
        {
            return 0;
        }

        public IDataReader GetData(int i)
        {
            return null;
        }

        public string GetDataTypeName(int i)
        {
            return this[i].GetType().BaseType.FullName;
        }

        public DateTime GetDateTime(int i)
        {
            return Convert.ToDateTime(this[i]);
        }

        public decimal GetDecimal(int i)
        {
            return Convert.ToDecimal(this[i]);
        }

        public double GetDouble(int i)
        {
            return Convert.ToDouble(this[i]);
        }

        public Type GetFieldType(int i)
        {
            return this[i].GetType();
        }

        public float GetFloat(int i)
        {
            return float.Parse(this[i].ToString());
        }

        public Guid GetGuid(int i)
        {
            return Guid.NewGuid();
        }

        public short GetInt16(int i)
        {
            return Convert.ToInt16(this[i]);
        }

        public int GetInt32(int i)
        {
            return Convert.ToInt32(this[i]);
        }

        public long GetInt64(int i)
        {
            return Convert.ToInt64(this[i]);
        }

        public string GetName(int i)
        {
            if (_dataSet.Tables.Count > 0)
            {
                return _dataSet.Tables[0].Columns[i].ColumnName;
            }

            return null;
        }

        public int GetOrdinal(string name)
        {
            if (_dataSet.Tables.Count > 0)
            {
                return _dataSet.Tables[0].Columns[name].Ordinal;
            }

            return -1;
        }

        public string GetString(int i)
        {
            return this[i].ToString();
        }

        public object GetValue(int i)
        {
            return this[i];
        }

        public int GetValues(object[] values)
        {
            return 0;
        }

        public bool IsDBNull(int i)
        {
            if (_dataSet.Tables.Count > 0)
            {
                return _dataSet.Tables[0].Rows[_indice - 1].IsNull(i);
            }

            return true;
        }

        public object this[string name]
        {
            get
            {
                if (_dataSet.Tables.Count > 0)
                {
                    return _dataSet.Tables[0].Rows[_indice - 1][name];
                }

                return null;
            }
        }

        public object this[int i]
        {
            get
            {
                if (_dataSet.Tables.Count > 0)
                {
                    return _dataSet.Tables[0].Rows[_indice - 1][i];
                }

                return null;
            }
        }
    }
}
